package com.loan.service;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Page;
import com.loan.model.Notice;
import com.loan.model.User;

public class NoticeService extends BaseService<User>{
	/**
	 * 分页数据
	 */ 
	public Page<Notice> list(int page, int size, Map<String, String> map) {
		return Notice.dao.paginate(page, size, buildSql("notice",Arrays.asList("id", "cid", "user_id", "create_time", "type", "status"),map," order by id desc "));
	} 
	
	/**
	 * 根据类型获取最新的一条数据
	 */
	public Notice findByType(String type, User u) {
		return Notice.dao.findFirst("select n.* from (select distinct * from notice where type=? and status='enable' order by id desc) n where n.id not in (select distinct notice_id from notice_log where user_id=?)", type, u.getId());
	}

	/**
	 * 关键词模糊搜索
	 * @param key
	 * @return
	 */
	public Page<Notice> findByKey(int page, int size, String key) {
		return Notice.dao.paginate(page, size, "select *", "from notice where  (contents like ? OR name like ?) AND type=?  ORDER BY create_time DESC", "%"+key+"%", "%"+key+"%", "repository");
	}
	
	/**
	 * 根据类型获取最新的一条数据
	 */
	public List<Notice> categories() {
		return Notice.dao.find("select summary as category from notice where status='enable' and type='help' group by summary order by create_time asc");
	}

	/**
	 * 根据分类获取数据
	 * @param category
	 */
	public List<Notice> findByCategory(String category) {
		return Notice.dao.find("select * from notice where  summary=? and type='help'  order by create_time asc", category);
	}
}
